﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ViCode_LeVi.Data;
using ViCode_LeVi.Langs;
using ViCode_LeVi.Utilities;
using System.Windows.Threading;

namespace ViCode_LeVi.Controls
{
    /// <summary>
    /// Interaction logic for VMTabTaskItemDetail.xaml
    /// </summary>
    public partial class VMTabTaskItemDetail : UserControl
    {
        public VMTabTaskItemDetail()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(VMTabTaskItemDetail_Loaded);
        }

        void VMTabTaskItemDetail_Loaded(object sender, RoutedEventArgs e)
        {
            LoadData4ComboBox();
            SetupShowDetail();
            SetupListView();
        }

        private void SetupListView()
        {
            ListView_Task_ResizeColumn();
            ListView_Tasks.SizeChanged -= new SizeChangedEventHandler(ListView_Tasks_SizeChanged);
            ListView_Tasks.SizeChanged += new SizeChangedEventHandler(ListView_Tasks_SizeChanged);
            ListView_Tasks.SourceUpdated += new EventHandler<DataTransferEventArgs>(ListView_Tasks_SourceUpdated);
        }

        void ListView_Tasks_SourceUpdated(object sender, DataTransferEventArgs e)
        {
            ListView_Task_ResizeColumn();
        }
        DispatcherTimer dis = null;
        private void ListView_Task_ResizeColumn()
        {
            SetupListView_Run();
            ThreadHelper.Runner.RunAfterUIAction(ListView_Tasks, () =>
            {
                SetupListView_Run();
            }, System.Windows.Threading.DispatcherPriority.DataBind);
            ThreadHelper.Runner.RunAfterUIAction(ListView_Tasks, () =>
            {
                SetupListView_Run();
            }, System.Windows.Threading.DispatcherPriority.Loaded);
            if (dis == null)
            {
                dis = new DispatcherTimer(new TimeSpan(0, 0, 2), DispatcherPriority.Loaded, (s, e) =>
                   {
                       dis.Stop();
                       SetupListView_Run();
                   }, this.Dispatcher);
            }
            dis.Start();
        }

        void ListView_Tasks_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            ListView_Task_ResizeColumn();
        }

        private void SetupListView_Run()
        {
            var total = ListView_Tasks.ActualWidth;
            var view = ListView_Tasks.View as GridView;
            if (view != null)
            {
                var columns = view.Columns.Where(c =>
                    !((c.Header != null && c.Header.ToString() == ControlTextInfo.Intance.TaskItem_Quantity_Text)
                    || (c.Header != null && c.Header.ToString() == ControlTextInfo.Intance.TaskItem_Quanlity_Text)
                    || (c.Header != null && c.Header.ToString() == ControlTextInfo.Intance.TaskItem_Systematic_Text))
                    )
                .ToArray();
                var usewidth = columns.Sum(c => c.ActualWidth);
                if (usewidth > 0)
                {
                    var width = ((total - usewidth) / 3) - 5;
                    if (width > 10)
                    {
                        GridViewColumn_Quanlity.Width = GridViewColumn_Quantity.Width = GridViewColumn_Systematic.Width = width;
                    }
                }
            }
        }

        #region SetupShowDetail
        private void SetupShowDetail()
        {
            ListView_Tasks.MouseDoubleClick -= new MouseButtonEventHandler(ListView_Tasks_MouseDoubleClick);
            ListView_Tasks.MouseDoubleClick += new MouseButtonEventHandler(ListView_Tasks_MouseDoubleClick);
            Button_Detail.Click -= new RoutedEventHandler(Button_Detail_Click);
            Button_Detail.Click += new RoutedEventHandler(Button_Detail_Click);
        }

        void Button_Detail_Click(object sender, RoutedEventArgs e)
        {
            Action_ShowDetail();
        }

        void ListView_Tasks_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            Action_ShowDetail();
        }
        void Action_ShowDetail()
        {
            if (ListView_Tasks.SelectedItem != null)
            {
                var task = ListView_Tasks.SelectedItem as Step_TaskItem;
                VMTaskItemDetail detail = new VMTaskItemDetail();//{ DataContext = ListView_Tasks.SelectedItem };
                detail.SetupDataContext(ListView_Tasks.SelectedItem as Step_TaskItem);
                Window w = new Window()
                {
                    Icon = Application.Current.MainWindow.Icon,
                    Content = detail
                        //, SizeToContent = SizeToContent.WidthAndHeight
                    ,
                    Title = string.Format("Detail of {0}", task.ID)
                    ,
                    WindowState = WindowState.Normal
                    , WindowStartupLocation = WindowStartupLocation.CenterScreen
                    ,
                    Height = System.Windows.SystemParameters.PrimaryScreenHeight * 8 / 9
                ,
                    Width = System.Windows.SystemParameters.PrimaryScreenWidth * 2 / 3
                };
                w.ShowDialog();
            }
        }
        #endregion

        #region LoadData4ComboBox
        private void LoadData4ComboBox()
        {
            if (RootData.Intance == null)
                return;
            var taskGroups = RootData.Intance.TaskItemGroupOfStep4;            
            #region ALL
            List<Step_TaskItem> list = new List<Step_TaskItem>();
            if (taskGroups != null)
            {
                foreach (var tg in taskGroups)
                {
                    list.AddRange(tg.Tasks_Source.ToArray());
                }
            }
            Step_TaskItemGroup taskAll = new Step_TaskItemGroup(list.ToArray()) { Name = ControlTextInfo.Intance.ComboBox_ItemAll_Text };
            #endregion

            if (taskGroups != null)
            {
                var listTaskGroups = new List<Step_TaskItemGroup>(taskGroups);
                listTaskGroups.Insert(0, taskAll);
                ComboBox_TaskType.ItemsSource = listTaskGroups;
                if (ComboBox_TaskType.Items.Count > 0)
                    ComboBox_TaskType.SelectedIndex = 0;
            }
        }
        #endregion

        #region IVMRefresh Members

        public void VMRefresh()
        {
            LoadData4ComboBox();
        }

        #endregion
    }
}
